<?php

abstract class modelosversoesDAOImpl {

    public static function Select($id_modelo=NULL, $id_versao=NULL) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select m.name as modelo, v.name as versao, mv.id_model, mv.id_version
                from models_versions mv
                inner join models m on m.id=mv.id_model
                inner join versions v on v.id = mv.id_version";

            if (isset($id_modelo) and isset($id_versao)) {
                $query = $query . " where id_model=? and id_version=?";
            }
            DB::$_stmt = DB::$mysqli->prepare($query);
            if (isset($id_modelo) and isset($id_versao)) {
                DB::$_stmt->bind_param('ii', $id_modelo, $id_versao);
            }
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result($data_modelo, $data_versao, $id_model, $id_version);
            while (DB::$_stmt->fetch()) {
                $data[] = array('modelo' => $data_modelo, 'versao' => $data_versao,'id_modelo'=>$id_model,'id_versao'=>$id_version);
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Insert($id_modelo,$id_versao) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "insert into models_versions
                (id_model,id_version) 
                values (?,?)";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('ii',$id_modelo,$id_versao);
            if (!DB::$_stmt->execute()) {
                echo DB::$mysqli->error;
            }
            return DB::$mysqli->insert_id;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Delete($id_modelo,$id_versao) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "delete from models_versions where id_model=? and id_version=?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('ii',$id_modelo,$id_versao);
            DB::$_stmt->execute();
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }

}

?>
